package com.belf.console.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.belf.common.utils.DateUtils;
import com.belf.console.domain.BelfTaskJob;
import com.belf.console.mapper.BelfTaskJobMapper;
import com.belf.console.service.IBelfTaskJobService;
import com.belf.quartz.domain.SysJob;
import com.belf.quartz.service.ISysJobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 任务调度器关联Service业务层处理
 * 
 * @author belf
 * @date 2024-09-12
 */
@Service
public class BelfTaskJobServiceImpl implements IBelfTaskJobService
{
    private static Logger LOGGER = LoggerFactory.getLogger(AppraiseTasksServiceImpl.class.getName());
    @Autowired
    private BelfTaskJobMapper belfTaskJobMapper;
    @Autowired
    private ISysJobService sysJobService;
    @Autowired
    private IBelfTaskJobService belfTaskJobService;

    /**
     * 查询任务调度器关联
     * 
     * @param id 任务调度器关联主键
     * @return 任务调度器关联
     */
    @Override
    public BelfTaskJob selectBelfTaskJobById(Long id)
    {
        return belfTaskJobMapper.selectBelfTaskJobById(id);
    }

    /**
     * 查询任务调度器关联列表
     * 
     * @param belfTaskJob 任务调度器关联
     * @return 任务调度器关联
     */
    @Override
    public List<BelfTaskJob> selectBelfTaskJobList(BelfTaskJob belfTaskJob)
    {
        return belfTaskJobMapper.selectBelfTaskJobList(belfTaskJob);
    }

    /**
     * 新增任务调度器关联
     * 
     * @param belfTaskJob 任务调度器关联
     * @return 结果
     */
    @Override
    public int insertBelfTaskJob(BelfTaskJob belfTaskJob)
    {
        return belfTaskJobMapper.insertBelfTaskJob(belfTaskJob);
    }

    /**
     * 修改任务调度器关联
     * 
     * @param belfTaskJob 任务调度器关联
     * @return 结果
     */
    @Override
    public int updateBelfTaskJob(BelfTaskJob belfTaskJob)
    {
        return belfTaskJobMapper.updateBelfTaskJob(belfTaskJob);
    }

    /**
     * 批量删除任务调度器关联
     * 
     * @param ids 需要删除的任务调度器关联主键
     * @return 结果
     */
    @Override
    public int deleteBelfTaskJobByIds(Long[] ids)
    {
        return belfTaskJobMapper.deleteBelfTaskJobByIds(ids);
    }

    /**
     * 删除任务调度器关联信息
     * 
     * @param id 任务调度器关联主键
     * @return 结果
     */
    @Override
    public int deleteBelfTaskJobById(Long id)
    {
        return belfTaskJobMapper.deleteBelfTaskJobById(id);
    }


    @Override
    public void insertJob(String monitorConf, Long id, String taskName, String taskType, Integer monitorStatus) {
        try {
            if (monitorConf == null){
                return;
            }
            SysJob sysJob = JSONObject.parseObject(monitorConf, SysJob.class);
            try {
            if (sysJob != null) {
                sysJob.setJobId(null);
//                sysJob.setCreateBy(SecurityUtils.getLoginUser().getUsername());
//                sysJob.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
                sysJob.setCreateTime(DateUtils.getNowDate());
                sysJob.setUpdateTime(DateUtils.getNowDate());
                sysJob.setJobName(taskType +taskName);
                sysJob.setStatus(String.valueOf(monitorStatus));
                String invokeTarget = buildInvokeTarget(sysJob.getJobGroup(), id);
                sysJob.setInvokeTarget(invokeTarget);
            }

                // 添加任务
                sysJobService.insertJob(sysJob);
                LOGGER.info("添加定时任务成功");
                // 添加关联
                BelfTaskJob belfTaskJob = new BelfTaskJob();
                belfTaskJob.setTaskId(id);
                belfTaskJob.setJobId(sysJob.getJobId());
                belfTaskJob.setTaskType(taskType);
                belfTaskJobService.insertBelfTaskJob(belfTaskJob);
            } catch (Exception e) {
                LOGGER.error("添加定时任务失败", e);
            }
        }catch (Exception e){
            LOGGER.error("添加定时任务失败", e);
        }
    }

    private String buildInvokeTarget(String groupType, Long id) {
        return switch (groupType) {
            case "5" -> "belfDataTask.appraiseTaskRun(" + id + "L)";
            case "6" -> "belfDataTask.dataTaskConfRun(" + id + "L)";
            case "7" -> "belfDataTask.dataCheckTaskRun(" + id + "L)";
            default -> "";
        };
    }

    @Override
    public void deleteTaskJob(Long id, String taskType) {
        BelfTaskJob belfTaskJob1 = new BelfTaskJob();

        belfTaskJob1.setTaskId(id);
        belfTaskJob1.setTaskType(taskType);
        List<BelfTaskJob> belfTaskJobList = belfTaskJobService.selectBelfTaskJobList(belfTaskJob1);
        belfTaskJobList.forEach(belfTaskJob -> {
            try {
                belfTaskJobService.deleteBelfTaskJobById(belfTaskJob.getId());
                SysJob sysJob = new SysJob();
                sysJob.setJobId(belfTaskJob.getJobId());
                sysJobService.deleteJob(sysJob);
            } catch (Exception e) {
                LOGGER.error("删除定时任务失败", e);
            }
        });
    }

}
